package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.LinkBean;
import com.project.service.ILinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("link")
public class LinkController {
    @Autowired
    private ILinkService service;
    @RequestMapping(value = "/findAll")
    public void findAll(@RequestParam(value = "pg",defaultValue = "1") int pageNO, HttpServletResponse resp) throws Exception{
        String str = "<table border='1' width=60%>";
        str+="<thead> <tr> <th> 名字</th><th>生日</th><th>电话</th><th>操作</th> </tr></thead><tbody>";
        PageInfo<LinkBean> list = service.cutPage(pageNO);
        for (LinkBean link:list.getList()){
            str+="<tr><td>"+link.getName()+"</td><td>"+link.getBirthday()+"</td><td>"+link.getPhone()+
                    "</td><td><a href='/link/del?id="+link.getId()+
                    "'>删除</a>  &nbsp;<a href='/link/findByID/"+link.getId()+"'>修改</a></td></tr>";
        }
        str+="</tbody></table>";
        str+="<a href = '/html/addLink.html'>添加用户</a><br>";
        for (int i=1;i<= list.getPages();i++){
            str+=" <a href= '/link/findAll?pg="+i+"'>"+i+"</a> &nbsp; ";
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println(str);
    }
    @RequestMapping("/add")
    public String add(LinkBean linkBean){
        service.add(linkBean);
        return "/link/findAll";
    }
    @RequestMapping("del")
    public String del(int id){
        service.del(id);
        return "redirect:/link/findAll";
    }
    @RequestMapping("update")
    public String update(int id,String phone){
        service.update(id,phone);
        return "/link/findAll";
    }
    @RequestMapping("findByID/{id}")
    public void findByID(@PathVariable("id") int id, HttpServletResponse resp)throws Exception{
        resp.setContentType("text/html;charset=utf-8");
        String str = "";
        LinkBean link= service.findById(id);
        str+="<form action='/link/update'>";
        str+="<input type='hidden' name='id' value='"+id+"'>";
        str+="姓名： "+link.getName()+"<br>"+"生日："+link.getBirthday()+"<br>";
        str+="电话：<input type='text' name='phone' value='"+link.getPhone()+"'><br>";
        str+="<input type='submit' value='修改'>";
       str+="</form>";
       resp.getWriter().println(str);
    }
    @RequestMapping("cutPage")
    public void cutPage(HttpServletResponse resp, HttpServletRequest req)throws Exception{
        resp.setContentType("text/html;charset=utf-8");

        String str = req.getParameter("pageNO");
        int pageNO = str == null ? 1 : Integer.parseInt(str);

        PageInfo<LinkBean> linkBeans=service.cutPage(pageNO);

        String infoStr = "<table border='1' width='60%'>";
        infoStr+="<thead><tr><th>姓名</th><th>生日</th><th>电话</th><th>操作</th></tr></thead>";
        infoStr+="<tbody>";

        for (LinkBean linkBean : linkBeans.getList()) {
            infoStr+="<tr>" +
                    "<td>"+linkBean.getName()+"</td>" +
                    "<td>"+linkBean.getBirthday()+"</td>" +
                    "<td>"+linkBean.getPhone()+"</td>" +
                    "<td><a href='/link/del?id="+linkBean.getId()+"'>删除</a>" +
                    " <a href='/link/findById?id="+linkBean.getId()+"'>修改</a>" +
                    "</td>" +
                    "</tr>";
        }

        infoStr+="</tbody>";
        infoStr+="</table>";
        //页码
        for (int i = 1; i <= linkBeans.getPages(); i++) {
            infoStr += "<a href='cutPage?pageNO=" + i + "'>" + i + "</a> ";
        }
        infoStr+="<br><a href='/html/addLink.html'>添加</a>";

        resp.getWriter().print(infoStr);
    }
}
